﻿/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * 
*     ___                              __                 _       __              __     __       *
*    /   |   _____ ____   ___   _____ /_/____   ____     | |     / /____   _____ / /____/ /_____  *
*   / /| |  / ___// __ \ / _ \ / ___// // __ \ / __ \    | | /| / // __ \ / ___// // __  // ___/  *
*  / ___ | _\_ \ / /_/ //  __// /   / // /_/ // / / /    | |/ |/ // /_/ // /   / // /_/ /_\_ \    *
* /_/  |_|/____// ____/ \___//_/   /_/ \____//_/ /_/     |__/|__/ \____//_/   /_/ \____//____/    *
*              /_/                                                                                *
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using System.IO;

namespace Worlds.Versions
{
    /// <summary>
    /// Versions Helper, helps check the version of a library file or assembly.
    /// </summary>
    public class VersionHelper
    {
        /// <summary>
        /// The Assembly that we are checking for.
        /// </summary>
        private Assembly MyFile { get; set; }
        /// <summary>
        /// The version information of the assembly.
        /// </summary>
        private FileVersionInfo MyVersionInfo { get; set; }
        /// <summary>
        /// The "String" version of the version information.
        /// </summary>
        private String MyVersion { get; set; }

        /// <summary>
        /// Constructs the version helper class and gets the version information.
        /// </summary>
        /// <param name="AssemblyPath">The path to the assembly file to check.</param>
        public VersionHelper(String AssemblyPath)
        {
            /* Check to see if the AssemblyPath is empty. */
            if (AssemblyPath == null || AssemblyPath == "")
                throw new NullReferenceException("Error loading library file, reason: The path to the file was empty.");

            /* Check to see if the file exists. */
            if (!File.Exists(AssemblyPath))
                throw new FileNotFoundException("Error loading library file, reason: The library file does not exist.");

            /* Setup our data now. */
            this.MyFile = Assembly.LoadFrom(AssemblyPath);
            this.MyVersionInfo = FileVersionInfo.GetVersionInfo(this.MyFile.Location);
            this.MyVersion = this.MyVersionInfo.ProductMajorPart + "." + this.MyVersionInfo.ProductMinorPart + "." + this.MyVersionInfo.ProductBuildPart;
        }

        /// <summary>
        /// Checks to see if the version passed is the same as the version we have loaded.
        /// </summary>
        /// <param name="RequiredVersion">The version that you require.</param>
        public void check(String RequiredVersion)
        {
            /* Verify that the versions match. */
            if (this.MyVersion != RequiredVersion)
                throw new DllNotFoundException(this.MyVersionInfo.OriginalFilename + " is out of date!" + Environment.NewLine + "Current Version: " + this.MyVersion + Environment.NewLine + "Required Version: " + RequiredVersion);
        }
    }
}
